package com.huchao.www.service.impl;

import com.huchao.www.dao.AdminMapper;
import com.huchao.www.dao.LockMapper;
import com.huchao.www.pojo.AdminInfo;
import com.huchao.www.pojo.LockInfo;
import com.huchao.www.service.api.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @Description:
 * @BelongsProject: springMVC
 * @BelongsPackage: com.huchao.www.service.impl
 * @Author: 亖思
 * @CreateTime: 2022/12/7 11:38
 * @Version: 1.0
 */

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private LockMapper lockMapper;
    /*
    锁定时间(三分钟)
     */
    Long lockTime = 180000L;
    /*
    时差
     */
    Long jetLag = null;


    /**
     * 〈登录验证〉
     *
     * @param adminInfo :用户实体
     * @param loginName :登录名
     * @Author: 亖思
     * @Date: 2022/12/7 21:43
     * @return: AdminInfo
     **/
    @Override
    public boolean doLogin(AdminInfo adminInfo,String loginName){
        AdminInfo admin = adminMapper.findByLoginAccount(adminInfo.getLoginName(), adminInfo.getPassword());
        LockInfo lockInfo = null;

        lockInfo = getLockInfo(loginName);

        System.out.println(lockInfo.getLockedTime());
        //密码错误
        if(admin == null){
            if(lockInfo.getLockStatus() == 0){
                //不锁定 判断该用户已失败的次数(除去本次)
                if(lockInfo.getFailCount()==2){
                    //说明这次是第三次 将该用户锁定，次数+1=3，并记录锁定时间
                    Date lastDate = new Date();
                    lockMapper.updateLockInfoByLoginName(1, lockInfo.getFailCount() + 1,lastDate ,lockInfo.getLoginName());
                    return false;
                }else{
                    //锁定未达到上限
                    lockMapper.updateLockInfoByLoginName(0, lockInfo.getFailCount() + 1,null,lockInfo.getLoginName());
                    return false;
                }
            }else{
                //锁定 判断时间
                Date nowDate = new Date();
                jetLag = nowDate.getTime() - lockInfo.getLockedTime().getTime();
                if(jetLag > lockTime){
                    //已过锁定时间  但是密码错误 清空错误记录 次数记1
                    lockMapper.updateLockInfoByLoginName(0, 1,null ,lockInfo.getLoginName());
                    return false;
                }else{
                    //还未过锁定时间,继续锁定
                    return false;
                }
            }
        }else{
            //正确，判断是否锁定
            if(lockInfo.getLockStatus() == 0){
                //不锁定
                lockMapper.updateLockInfoByLoginName(0, 0,null ,lockInfo.getLoginName());
                return true;
            }else{
                //锁定，判断锁定时间
                Date nowDate = new Date();
                jetLag = nowDate.getTime() - lockInfo.getLockedTime().getTime();
                if(jetLag > lockTime){
                    //已过锁定时间 密码正确 清空记录
                    lockMapper.updateLockInfoByLoginName(0, 0,null ,lockInfo.getLoginName());
                    return true;
                }else{
                    //还未过锁定时间,继续锁定
                    return false;
                }
            }
        }
    }

    /**
     * 〈获取锁定信息〉
     * 获取数据库里的锁定信息，没有则新增
     * @param loginName:用户名
     * @Author: 亖思
     * @Date: 2022/12/9 11:20
     * @return: LockInfo
     **/
    public LockInfo getLockInfo(String loginName){
        //锁定表里没有此用户名
        if(lockMapper.findByLoginName(loginName) == null){
            //增加该用户
            lockMapper.insertNewInfo(loginName);
        }
        return lockMapper.findByLoginName(loginName);
    }
}
